package com.zm.demo.javaagent.attach;

import com.sun.tools.attach.VirtualMachine;
import com.sun.tools.attach.VirtualMachineDescriptor;

import java.io.File;
import java.util.List;

/**
 * @author zoum
 * @create 2019/12/26 15:00
 */
public class AttachTest {

    public static void main(String[] args) throws Exception {

        System.out.println(System.getProperty("user.dir"));

        // new AttachThread(System.getProperty("user.dir") + JAR_PATH,
        // VirtualMachine.list()).start();

        List<VirtualMachineDescriptor> vmList = VirtualMachine.list();

        System.out.println("当前已经启动的JVM");
        for (VirtualMachineDescriptor virtualMachineDescriptor : vmList) {
            System.out.println(virtualMachineDescriptor.id() + "\t" + virtualMachineDescriptor);
        }


        //指定jar路径
        String agentFilePath = "E:\\git\\zm\\demo\\javaagent-simple\\target\\javaagent-simple-1.0-SNAPSHOT-jar-with-dependencies.jar";

        File agentFile = new File(agentFilePath);
        try {
            VirtualMachine jvm = VirtualMachine.attach("12604");
            jvm.loadAgent(agentFile.getAbsolutePath());
            jvm.detach();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
